package com.statistics.controller;

import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.constant.HttpStatus;
import com.common.core.controller.BaseController;
import com.common.core.page.TableDataInfo;
import com.statistics.service.EducationService;
import com.statistics.service.FixedReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 科研统计
 *
 * @author chenyy
 */

@Api(tags = "科研统计")
@Anonymous
@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {
    @Resource
    private FixedReportService fixedReportService;
    @Resource
    private EducationService educationService;

    /**
     * 固定报表
     *
     * @param reportClass 固定报表类型
     * @param beginYear   开始年份
     * @param endYear     结束年份
     * @return TableDataInfo
     **/
    @GetMapping("/getFixedReport")
    @ApiOperation(value = "固定报表", notes = "固定报表类型<br> 1:各单位成果类型年度统计表<br> 2:各单位获奖情况年度统计表<br> " +
            "3:各单位科研人员职称_学历_学位分析表<br> 4:各年份项目数量统计表[按合同级别][横向项目]<br> 5:各年份项目数量统计表[按项目级别][纵向项目]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportClass", value = "固定报表类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "beginYear", value = "开始年份", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endYear", value = "结束年份", dataType = "int", paramType = "query")
    })
    public TableDataInfo getReportByFixed(int reportClass, @Nullable Integer beginYear, @Nullable Integer endYear) {
        switch (reportClass) {
            case 1:
                // 各单位成果类型年度统计表
                JSONObject json = fixedReportService.getUnitAchievement(beginYear, endYear, 1);
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(HttpStatus.SUCCESS);
                rspData.setMsg("查询成功");
                rspData.setRows(json.getJSONArray("data"));
                rspData.setTotal(json.getLongValue("total"));
                return rspData;
            case 2:
                // 各单位获奖情况年度统计表
                startPage();
                return getDataTable(fixedReportService.getUnitHonor(beginYear, endYear));
            case 3:
                // 各单位科研人员职称_学历_学位分析表
                startPage();
                return getDataTable(fixedReportService.getUnitPersonType());
            case 4:
                // 各年份项目数量统计表[按合同级别][横向项目]
                startPage();
                return getDataTable(fixedReportService.getHxProjectNum(beginYear, endYear));
            case 5:
                // 各年份项目数量统计表[按项目级别][纵向项目]
                startPage();
                return getDataTable(fixedReportService.getZxProjectNum(beginYear, endYear));
            default:
                return getDataTableError();
        }

    }

    /**
     * 固定报表导出
     *
     * @param reportClass 固定报表类型
     * @param beginYear   开始年份
     * @param endYear     结束年份
     **/
    @GetMapping("/fixedReportExport")
    @ApiOperation(value = "固定报表导出", notes = "固定报表类型<br> 1:各单位成果类型年度统计表<br> 2:各单位获奖情况年度统计表<br> " +
            "3:各单位科研人员职称_学历_学位分析表<br> 4:各年份项目数量统计表[按合同级别][横向项目]<br> 5:各年份项目数量统计表[按项目级别][纵向项目]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportClass", value = "固定报表类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "beginYear", value = "开始年份", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endYear", value = "结束年份", dataType = "int", paramType = "query")
    })
    public void fixedReportExport(HttpServletResponse response, int reportClass, int beginYear, int endYear) throws Exception {
        switch (reportClass) {
            case 1:
                // 各单位成果类型年度统计表
                fixedReportService.getUnitAchievementExport(beginYear, endYear, response);
                break;
            case 2:
                // 各单位获奖情况年度统计表
                fixedReportService.getUnitHonorExport(beginYear, endYear, response);
                break;
            case 3:
                fixedReportService.getUnitPersonTypeExport(response);
                break;
            case 4:
                fixedReportService.getHxProjectNumExport(beginYear, endYear, response);
                break;
            case 5:
                fixedReportService.getZxProjectNumExport(beginYear, endYear, response);
                break;
        }
    }

    @GetMapping("/education")
    @ApiOperation(value = "教育部年度统计", notes = "type[1：社科类, 2：科技类]<br> " +
            "subType[院系所数据导出, 社科活动人员数据导出, 科研成果数据导出, 科研项目数据导出, 成果获奖数据导出, 研究机构数据导出, 项目年度工作量数据导出, 项目年度经费数据导出, " +
            "科技人力资源数据导出, 科技经费数据导出, 科技活动机构数据导出, 科技项目(课题)数据导出, 科技交流数据导出, 转让与知识产权情况导出, 科技成果数据导出, 出版科技著作数据导出, 科技成果奖励数据导出, 科技期刊数据导出]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年度", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类别", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "subType", value = "子类", required = true, dataType = "string", paramType = "query")
    })
    public void education(HttpServletResponse response, String year, Integer type, String subType) throws Exception {
        switch (subType) {
            case "院系所数据导出":
                educationService.exportUnit(response, type);
                break;
            case "社科活动人员数据导出":
                educationService.exportPerson(response);
                break;
            case "科研成果数据导出":
                educationService.exportResAchieve(response, year, type);
                break;
            case "科研项目数据导出":
                educationService.exportProject(response, year, type);
                break;
            case "成果获奖数据导出":
                educationService.exportHonor(response, year, type);
                break;
            case "研究机构数据导出":
                educationService.exportResUnit(response, year, type);
                break;
            case "科技人力资源数据导出":
                educationService.exportResPerson(response);
                break;
            case "科技活动机构数据导出":
                educationService.exportActivityUnit(response, year, type);
                break;
            case "科技项目(课题)数据导出":
                educationService.exportScienceProject(response, year, type);
                break;
            case "科技交流数据导出":
                educationService.exportScienceInteract(response, year);
                break;
            case "科技成果数据导出":
                educationService.exportScienceAchievement(response, year, type);
                break;
            case "出版科技著作数据导出":
                educationService.exportPublishBook(response, year, type);
                break;
            case "科技成果奖励数据导出":
                educationService.exportScienceHonor(response, year, type);
                break;
            case "项目年度工作量数据导出":
            case "项目年度经费数据导出":
            case "科技经费数据导出":
            case "技术转让与知识产权情况导出":
            case "科技期刊数据导出":
                educationService.downloadTemplate(response, subType);
                break;
        }
    }

}